package activity.setting;

import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.hichip.callback.ICameraIOSessionCallback;
import com.hichip.campro.R;
import com.hichip.content.HiChipDefines;
import com.hichip.control.HiCamera;
import java.io.ByteArrayOutputStream;
import java.util.regex.Pattern;
import activity.FishEye.FishEyeActivity;
import activity.WallMounted.WallMountedActivity;
import activity.live.LiveViewActivity;
import activity.setting.PlanVideo.PlanVideoActivity;
import base.HiActivity;
import bean.MyCamera;
import butterknife.BindView;
import common.CamHiDefines;
import common.HiDataValue;
import custom.ShadowLayout;
import custom.dialog.NiftyDialogBuilder;
import jp.wasabeef.glide.transformations.BlurTransformation;
import utils.HiToast;
import utils.HiTools;
import utils.SharePreUtils;
/**
 * Created by lt on 2017\12\29 0029.
 */
@RequiresApi(api = Build.VERSION_CODES.M)
public class SettingActivity extends HiActivity implements View.OnClickListener, View.OnScrollChangeListener, ICameraIOSessionCallback {
    @BindView(R.id.iv_head) ImageView iv_head;
    @BindView(R.id.iv_return) ImageView iv_return;
    @BindView(R.id.iv_head_icon) ImageView iv_head_icon;
    @BindView(R.id.scrollView) ScrollView scrollView;
    @BindView(R.id.et_nickname) EditText et_nickname;
    @BindView(R.id.iv_edit_name) ImageView iv_edit_name;
    @BindView(R.id.tv_uid) TextView tv_uid;
    @BindView(R.id.ll_setting_wifi) LinearLayout ll_setting_wifi;
    @BindView(R.id.ll_setting_password) LinearLayout ll_setting_password;
    @BindView(R.id.ll_setting_device_info) LinearLayout ll_setting_device_info;
//    @BindView(R.id.ll_setting_alarm_action) LinearLayout ll_setting_alarm_action;
    @BindView(R.id.ll_setting_system) LinearLayout ll_setting_system;
    @BindView(R.id.ll_setting_motion_detect) LinearLayout ll_setting_motion_detect;
    @BindView(R.id.ll_setting_video) LinearLayout ll_setting_video;
    @BindView(R.id.ll_setting_sd_card) LinearLayout ll_setting_sd_card;
    @BindView(R.id.ll_setting_plan_video) LinearLayout ll_setting_plan_video;
    @BindView(R.id.rl_setting_audio) RelativeLayout rl_setting_audio;
    @BindView(R.id.ll_setting_time) LinearLayout ll_setting_time;
    @BindView(R.id.rl_head_name) RelativeLayout rl_head_name;
    @BindView(R.id.et_head_nickname) EditText et_head_nickname;
    @BindView(R.id.tv_head_uid) TextView tv_head_uid;
    @BindView(R.id.btn_delete_camera) TextView btn_delete_camera;

    @BindView(R.id.ll_all_setting) LinearLayout ll_all_setting;
    @BindView(R.id.tv_disconnect_1) TextView tv_disconnect_1;

    @BindView(R.id.tv_current_device_wifi) TextView tv_current_device_wifi;
    @BindView(R.id.ll_alarm_setting) LinearLayout ll_alarm_setting;//报警管理与通知

    @BindView(R.id.shadowlayout) ShadowLayout shadowlayout;
    private MyCamera mMyCamera;
    private int mTop;
    private int lastDis;
    private byte[] bytes;
    @BindView(R.id.ll_hand_edit) LinearLayout ll_hand_edit;

    private Bitmap bitmap;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        HiTools.setTransStatusBar(this);
        getIntentData();
        initViewAndData(); //点击设置,781机器耗时在这里面
        setListeners();

    }

    private float alpha = 0.4f;


    @Override
    protected void onResume() {
        super.onResume();
        if(bitmap==null){
            initHeadView();
        }
        if (mMyCamera != null) {
            mMyCamera.registerIOSessionListener(this);
        }
        if (mMyCamera.getConnectState() != HiCamera.CAMERA_CONNECTION_STATE_LOGIN) {
            ll_setting_wifi.setEnabled(false);
            ll_setting_wifi.setAlpha(alpha);
            ll_setting_password.setEnabled(false);
            ll_setting_password.setAlpha(alpha);
            ll_setting_system.setEnabled(false);
            ll_setting_system.setAlpha(alpha);
            ll_setting_device_info.setEnabled(false);
            ll_setting_device_info.setAlpha(alpha);
            ll_alarm_setting.setEnabled(false);
            ll_alarm_setting.setAlpha(alpha);
            ll_setting_motion_detect.setEnabled(false);
            ll_setting_motion_detect.setAlpha(alpha);
            ll_setting_plan_video.setEnabled(false);
            ll_setting_plan_video.setAlpha(alpha);
            ll_setting_sd_card.setEnabled(false);
            ll_setting_sd_card.setAlpha(alpha);
            ll_setting_video.setEnabled(false);
            ll_setting_video.setAlpha(alpha);
            rl_setting_audio.setEnabled(false);
            rl_setting_audio.setAlpha(alpha);
            ll_setting_time.setEnabled(false);
            ll_setting_time.setAlpha(alpha);
            if (mMyCamera.getConnectState() == HiCamera.CAMERA_CONNECTION_STATE_DISCONNECTED) {
                ll_all_setting.setVisibility(View.GONE);
                tv_disconnect_1.setVisibility(View.VISIBLE);
            }
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void setListeners() {
        iv_return.setOnClickListener(this);
        iv_edit_name.setOnClickListener(this);
        ll_setting_wifi.setOnClickListener(this);
        ll_setting_password.setOnClickListener(this);
        ll_setting_device_info.setOnClickListener(this);
//        ll_setting_alarm_action.setOnClickListener(this);
        ll_setting_system.setOnClickListener(this);
        ll_setting_motion_detect.setOnClickListener(this);
        ll_setting_video.setOnClickListener(this);
        ll_setting_sd_card.setOnClickListener(this);
        ll_setting_plan_video.setOnClickListener(this);
        ll_setting_time.setOnClickListener(this);
        scrollView.setOnScrollChangeListener(this);
        btn_delete_camera.setOnClickListener(this);
        iv_head_icon.setOnClickListener(this);
        ll_hand_edit.setOnClickListener(this);
        rl_setting_audio.setOnClickListener(this);
        ll_alarm_setting.setOnClickListener(this);
    }


    private void initViewAndData() {
        mTop = HiTools.dip2px(SettingActivity.this, 105);
        lastDis = HiTools.dip2px(SettingActivity.this, 5);
        et_nickname.setText(mMyCamera.getNikeName());
        et_head_nickname.setText(mMyCamera.getNikeName());
        tv_uid.setText(mMyCamera.getUid());
        tv_head_uid.setText(mMyCamera.getUid());
    }


    private void initHeadView() {
        bitmap=mMyCamera.snapshot;
        new Thread() {
            @Override
            public void run() {
                if (mMyCamera.snapshot != null) {
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    mMyCamera.snapshot.compress(Bitmap.CompressFormat.PNG, 100, baos);
                    bytes = baos.toByteArray();
                }
                mHandler.sendEmptyMessage(0X998);
            }
        }.start();
        if (mMyCamera.snapshot != null) {
            iv_head_icon.setImageBitmap(mMyCamera.snapshot);
        } else {
            iv_head_icon.setImageResource(R.mipmap.videoclip);
        }
    }

    private void getIntentData() {
        String str_uid = getIntent().getStringExtra(HiDataValue.EXTRAS_KEY_UID);
        if (TextUtils.isEmpty(str_uid)) {
            HiToast.showToast(this, "UID is Empty");
            SettingActivity.this.finish();
            return;
        }
        for (MyCamera camera : HiDataValue.CameraList) {
            if (str_uid.equalsIgnoreCase(camera.getUid())) {
                this.mMyCamera = camera;
                break;
            }
        }
        if (HiTools.cameraWhetherNull(mMyCamera,this)) return;
        if (mMyCamera.getCommandFunction(HiChipDefines.HI_P2P_GET_WIFI_PARAM_NEWPWD255_EXT1)) {
            mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_GET_WIFI_PARAM_NEWPWD255_EXT1, new byte[0]);
            mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_GET_WIFI_PARAM, null);
        }
    }


    @Override
    public void onClick(View v) {
        if (et_nickname.hasFocus()) {
            if (v.getId() == R.id.iv_return) {
                SettingActivity.this.finish();
            } else {
                handDown();
                return;
            }
        }
        switch (v.getId()) {
            case R.id.iv_head_icon:
                if (mMyCamera != null && mMyCamera.getConnectState() == HiCamera.CAMERA_CONNECTION_STATE_LOGIN) {
                    mMyCamera.isFirstEnterLive = SharePreUtils.getBoolean(HiDataValue.CACHE, this, mMyCamera.getUid() + "isFirstEnterLive", true);
                    if(mMyCamera.isWallMounted){// 壁装新镜头
                        Intent intent=new Intent(this, WallMountedActivity.class);
                        intent.putExtra(HiDataValue.EXTRAS_KEY_UID,mMyCamera.getUid());
                        startActivity(intent);
                        return;
                    }else if(mMyCamera.isFishEye()){
                        // 初始化鱼眼顶装和壁装的模式
                        int num = SharePreUtils.getInt("mInstallMode", this, mMyCamera.getUid());
                        mMyCamera.mInstallMode = num == -1 ? 0 : num;
                        Intent intent=new Intent(this,FishEyeActivity.class);
                        intent.putExtra(HiDataValue.EXTRAS_KEY_UID,mMyCamera.getUid());
                        startActivity(intent);
                        return;
                    }
                    //转场动画设置
                    //Intent intent = new Intent(SettingActivity.this, LiveViewActivity.class);
                    Intent intent = new Intent(SettingActivity.this, LiveViewActivity.class);
                    ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this, iv_head_icon, "iv");
                    intent.putExtra(HiDataValue.EXTRAS_KEY_UID, mMyCamera.getUid());
                    startActivity(intent, options.toBundle());
                } else {
                    HiToast.showToast(SettingActivity.this, getString(R.string.current_disconnect));
                }
                break;
            case R.id.iv_return:
                super.onBackPressed();
                break;
            case R.id.iv_edit_name:
                handIvEditName();
                break;
            case R.id.ll_setting_wifi://WIFI设置
                Intent intent = new Intent(this, WiFiSettingActivity.class);
                intent.putExtra(HiDataValue.EXTRAS_KEY_UID, mMyCamera.getUid());
                startActivity(intent);
                break;
            case R.id.ll_setting_password://修改密码
                intent = new Intent(this, PasswordSettingActivity.class);
                intent.putExtra(HiDataValue.EXTRAS_KEY_UID, mMyCamera.getUid());
                startActivity(intent);
                break;
            case R.id.ll_setting_device_info://设备信息
                intent = new Intent(this, DeviceInfoActivity.class);
                intent.putExtra(HiDataValue.EXTRAS_KEY_UID, mMyCamera.getUid());
                startActivity(intent);
                break;
            case R.id.ll_setting_system://系统设置
                intent = new Intent(this, SystemSettingActivity.class);
                intent.putExtra(HiDataValue.EXTRAS_KEY_UID, mMyCamera.getUid());
                startActivity(intent);
                break;
            case R.id.ll_setting_motion_detect://移动侦测
                if (mMyCamera.getCommandFunction(HiChipDefines.HI_P2P_SET_MD_PARAM_NEW)) {
                    intent = new Intent(this, MotionDetectExtActivity.class);
                } else {
                    intent = new Intent(this, MotionDetectActivity.class);
                }
                intent.putExtra(HiDataValue.EXTRAS_KEY_UID, mMyCamera.getUid());
                startActivity(intent);
                break;
            case R.id.ll_setting_plan_video://SD 卡计划录像
                //intent = new Intent(this, PlanVideoActivity.class);
                //intent = new Intent(this, PlanVideoActivity2.class);
                intent = new Intent(this, PlanVideoActivity.class);
                intent.putExtra(HiDataValue.EXTRAS_KEY_UID, mMyCamera.getUid());
                startActivity(intent);
                break;
            case R.id.ll_setting_sd_card://SD卡设置
                intent = new Intent(this, SDCardSettingActivity.class);
                intent.putExtra(HiDataValue.EXTRAS_KEY_UID, mMyCamera.getUid());
                startActivity(intent);
                break;
            case R.id.ll_setting_video://视频设置
                intent = new Intent(this, VideoSettingActivity.class);
                intent.putExtra(HiDataValue.EXTRAS_KEY_UID, mMyCamera.getUid());
                startActivity(intent);
                break;
            case R.id.ll_setting_time://设备时间设置
                intent = new Intent(this, TimeSettingActivity.class);
                intent.putExtra(HiDataValue.EXTRAS_KEY_UID, mMyCamera.getUid());
                startActivity(intent);
                break;
            case R.id.btn_delete_camera://删除摄像机
                showDeleteCamera();
                break;
            case R.id.rl_setting_audio:
                intent=new Intent(SettingActivity.this,AudioSettingActivity.class);
                intent.putExtra(HiDataValue.EXTRAS_KEY_UID,mMyCamera.getUid());
                startActivity(intent);
                break;
            case R.id.ll_alarm_setting://报警管理与通知
                intent=new Intent(SettingActivity.this,AlarmManageAndNotiActivity.class);
                intent.putExtra(HiDataValue.EXTRAS_KEY_UID,mMyCamera.getUid());
                startActivity(intent);
                break;
        }
    }

    private void showPupTip() {
        View customView = View.inflate(this, R.layout.pup_please_connect_net, null);
        PopupWindow popupWindow = new PopupWindow(customView);
        ColorDrawable cd = new ColorDrawable(-0000);
        popupWindow.setBackgroundDrawable(cd);
        popupWindow.setOutsideTouchable(false);
        popupWindow.setFocusable(true);
        popupWindow.setHeight(WindowManager.LayoutParams.MATCH_PARENT);
        popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        popupWindow.showAtLocation(customView, Gravity.CENTER, 0, 0);
        //ok
        TextView tv_ok = customView.findViewById(R.id.tv_ok);
        tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
    }

    private void showDeleteCamera() {
        // NiftyDialogBuilder dialog = new NiftyDialogBuilder(this, R.style.CustomDialog);
        NiftyDialogBuilder dialog = NiftyDialogBuilder.getInstance(this);
        dialog.withTitle(getString(R.string.confirm_delete)).withMessage(mMyCamera.getUid() + getString(R.string.newline_delete)).withButton1Text(getString(R.string.cancel)).withButton2Text(getString(R.string.delete));
        dialog.setButton1Click(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.setButton2Click(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                showjuHuaDialog();
                mMyCamera.bindPushState(false, bindPushResult);
                SharePreUtils.removeKey(HiDataValue.CACHE, SettingActivity.this, mMyCamera.getUid());
                SharePreUtils.putBoolean(HiDataValue.CACHE, SettingActivity.this, "isFirstPbOnline", false);
                SharePreUtils.putBoolean(HiDataValue.CACHE, SettingActivity.this, mMyCamera.getUid() + "pb", false);
                sendUnRegister(mMyCamera, 0);
                Message msg = mHandler.obtainMessage();
                msg.what = HiDataValue.HANDLE_MESSAGE_DELETE_FILE;
                msg.obj = mMyCamera;
                mHandler.sendMessageDelayed(msg, 1000);
            }
        });
        dialog.show();
    }

    private void handIvEditName() {
        et_nickname.setFocusable(true);
        et_nickname.setFocusableInTouchMode(true);
        et_nickname.requestFocus();
        if (!TextUtils.isEmpty(et_nickname.getText().toString())) {
            et_nickname.setSelection(et_nickname.getText().toString().length());
        }
        InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(et_nickname, InputMethodManager.SHOW_FORCED);
    }

    @Override
    public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
        Log.i("tedu", "--onScrollChange--scrollY--:" + scrollY + "--scrollX--:" + scrollX);
        Log.i("tedu", "--mTop--:" + mTop);
        if (scrollY <= mTop) {
            rl_head_name.animate().translationY(-scrollY).setDuration(0).start();
            float scale = (1 - (float) scrollY / mTop) - 0.1f;
            if (scrollY < lastDis) {
                scale = 1f;
            }
            shadowlayout.animate().scaleX(scale).scaleY(scale).setDuration(0).start();
            shadowlayout.animate().alpha(scale).setDuration(0).start();
        } else {
            rl_head_name.animate().translationY(-mTop).setDuration(0).start();
            shadowlayout.animate().scaleX(0f).scaleY(0f).setDuration(0).start();
            shadowlayout.animate().alpha(0f).setDuration(0).start();
        }
    }
    //点击空白处 确认摄像机名称的修改
    private boolean handDown() {
        et_nickname.clearFocus();
        et_nickname.setFocusable(false);
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(et_nickname.getWindowToken(), 0);

        String str_nick = et_nickname.getText().toString();
        if (str_nick.length() == 0) {
            showAlert(getText(R.string.tips_null_nike));
            return true;
        }
        String strNickName = mMyCamera.getNikeName();
        if (!strNickName.equals(str_nick)) {
            mMyCamera.setNikeName(str_nick);
            mMyCamera.updateInDatabase(this);
        }
        return false;
    }

    MyCamera.OnBindPushResult bindPushResult = new MyCamera.OnBindPushResult() {
        @Override
        public void onBindSuccess(MyCamera camera) {

            if (!camera.handSubXYZ()) {
                camera.setServerData(HiDataValue.CAMERA_ALARM_ADDRESS);
            } else {
                camera.setServerData(HiDataValue.CAMERA_ALARM_ADDRESS_THERE);
            }
            camera.updateServerInDatabase(SettingActivity.this);
            sendServer(camera);
            sendRegisterToken(camera);
        }

        @Override
        public void onBindFail(MyCamera camera) {
        }

        @Override
        public void onUnBindSuccess(MyCamera camera) {
            camera.bindPushState(true, bindPushResult);
        }

        @Override
        public void onUnBindFail(MyCamera camera) {
            // 把SubId存放到sharePrefence
            if (camera.getPushState() > 0) {
                SharePreUtils.putInt("subId", SettingActivity.this, camera.getUid(), camera.getPushState());
            }

        }

    };

    protected void sendServer(MyCamera mCamera) {
        // //测试
        // mCamera.sendIOCtrl(CamHiDefines.HI_P2P_ALARM_ADDRESS_GET, null);
        if (mCamera.getServerData() == null) {
            mCamera.setServerData(HiDataValue.CAMERA_ALARM_ADDRESS);
            mCamera.updateServerInDatabase(SettingActivity.this);
        }
        if (!mCamera.getCommandFunction(CamHiDefines.HI_P2P_ALARM_ADDRESS_SET)) {
            return;
        }
        if (mCamera.push != null) {
            String[] strs = mCamera.push.getPushServer().split("\\.");
            if (strs.length == 4 && isInteger(strs[0]) && isInteger(strs[1]) && isInteger(strs[2]) && isInteger(strs[3])) {
                byte[] info = CamHiDefines.HI_P2P_ALARM_ADDRESS.parseContent(mCamera.push.getPushServer());
                mCamera.sendIOCtrl(CamHiDefines.HI_P2P_ALARM_ADDRESS_SET, info);
            }

        }
    }

    /*
     * 推荐，速度最快 判断是否为整数
     * @param str 传入的字符串
     * @return 是整数返回true,否则返回false
     */
    public static boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }

    private void sendUnRegister(MyCamera mCamera, int enable) {
        if (mCamera.getPushState() == 1) {
            return;
        }
        if (!mCamera.getCommandFunction(CamHiDefines.HI_P2P_ALARM_TOKEN_UNREGIST)) {
            return;
        }
        byte[] info = CamHiDefines.HI_P2P_ALARM_TOKEN_INFO.parseContent(0, mCamera.getPushState(), (int) (System.currentTimeMillis() / 1000 / 3600), enable);
        mCamera.sendIOCtrl(CamHiDefines.HI_P2P_ALARM_TOKEN_UNREGIST, info);
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case HiDataValue.HANDLE_MESSAGE_RECEIVE_IOCTRL:
                    handIOCTRL(msg);
                    break;
                case 0X998:
                    if (bytes != null) {
                        Glide.with(SettingActivity.this).load(bytes).placeholder(R.mipmap.videoclip).error(R.mipmap.videoclip).bitmapTransform(new BlurTransformation(SettingActivity.this, 23, 5)).into(iv_head);
                    }
                    break;
                case HiDataValue.HANDLE_MESSAGE_DELETE_FILE:
                    mMyCamera.disconnect();
                    mMyCamera.deleteInCameraList();
                    mMyCamera.deleteInDatabase(SettingActivity.this);
                    //adapter.notifyDataSetChanged();
                    dismissjuHuaDialog();
                    HiToast.showToast(SettingActivity.this, getString(R.string.tips_remove_success));
                    sendBroadcast(new Intent(HiDataValue.ACTION_CAMERA_INIT_END));
                    finish();
                    break;
            }

        }
    };

    private void handIOCTRL(Message msg) {
        if (msg.arg2 == 0) {
            Bundle bundle = msg.getData();
            byte[] data = bundle.getByteArray(HiDataValue.EXTRAS_KEY_DATA);
            switch (msg.arg1) {
                case HiChipDefines.HI_P2P_GET_WIFI_PARAM:
                    HiChipDefines.HI_P2P_S_WIFI_PARAM wifi_param = new HiChipDefines.HI_P2P_S_WIFI_PARAM(data);
                    String ssid = new String(wifi_param.strSSID);
                    if (!TextUtils.isEmpty(ssid)) {
                        tv_current_device_wifi.setText(ssid.trim());
                    }
                    break;
                case HiChipDefines.HI_P2P_GET_WIFI_PARAM_NEWPWD255_EXT1:
                    HiChipDefines.HI_P2P_S_WIFI_PARAM_NEWPWD255_EXT1 ext1 = new HiChipDefines.HI_P2P_S_WIFI_PARAM_NEWPWD255_EXT1(data);
                    ssid = new String(ext1.strSSID);
                    if (!TextUtils.isEmpty(ssid)) {
                        tv_current_device_wifi.setText(ssid.trim());
                    }
                    break;
                case HiChipDefines.HI_P2P_SET_AUDIO_ATTR:
                    dismissjuHuaDialog();
                    break;
            }
        } else {

        }
    }

    protected void sendRegisterToken(MyCamera mCamera) {
        if (mCamera.getPushState() == 1 || mCamera.getPushState() == 0) {
            return;
        }
        if (!mCamera.getCommandFunction(CamHiDefines.HI_P2P_ALARM_TOKEN_REGIST)) {
            return;
        }
        byte[] info = CamHiDefines.HI_P2P_ALARM_TOKEN_INFO.parseContent(0, mCamera.getPushState(), (int) (System.currentTimeMillis() / 1000 / 3600), 1);
        mCamera.sendIOCtrl(CamHiDefines.HI_P2P_ALARM_TOKEN_REGIST, info);
    }

    @Override
    protected void onPause() {
        super.onPause();
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        boolean isOpen = imm.isActive();
        if (isOpen) {
            imm.hideSoftInputFromWindow(et_nickname.getWindowToken(), 0);
        }
        if (mMyCamera != null) {
            mMyCamera.unregisterIOSessionListener(this);
        }

    }

    @Override
    public void receiveSessionState(HiCamera hiCamera, int i) {

    }

    @Override
    public void receiveIOCtrlData(HiCamera arg0, int arg1, byte[] arg2, int arg3) {
        if (arg0 != mMyCamera) return;
        Bundle bundle = new Bundle();
        bundle.putByteArray(HiDataValue.EXTRAS_KEY_DATA, arg2);
        Message msg = mHandler.obtainMessage();
        msg.what = HiDataValue.HANDLE_MESSAGE_RECEIVE_IOCTRL;
        msg.obj = arg0;
        msg.arg1 = arg1;
        msg.arg2 = arg3;
        msg.setData(bundle);
        mHandler.sendMessage(msg);
    }

//    private   int spaceTime = 2000;//时间间隔
//    private   long lastClickTime=0;//上次点击的时间
//
//    private  boolean isFastClick(){
//        long currentTime=System.currentTimeMillis();
//        boolean isAllowClick=false;
//        if(currentTime-lastClickTime>spaceTime){
//            isAllowClick=true;
//        }else {
//            isAllowClick=false;
//        }
//        lastClickTime=currentTime;
//        return isAllowClick;
//    }


}
